using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Keep.Const;
using Keep.Model;
using Keep.Util;

namespace Keep.Service.Impl;

public class LinkServiceImpl : ILinkService
{
    public void AddLinkClick(int id)
    {
        SqlSugarHelper.Db.Updateable<Links>()
            .SetColumns(it => it.num == it.num + 1)
            .Where(it => it.id == id)
            .ExecuteCommand();
    }

    public bool AddLinks(Links links)
    {
        links.num = 0;
        links.status = Static.Enable;
        int i = SqlSugarHelper.Db.Insertable(links).ExecuteCommand();
        return i > 0;
    }

    public bool DelLinks(int id)
    {
        int i = SqlSugarHelper.Db.Updateable<Links>()
                .SetColumns(it => it.status == Static.Disable)
                .Where(lt => lt.id == id)
                .ExecuteCommand();
        return i > 0;
    }

    public bool EditLinks(Links links)
    {
        int i = SqlSugarHelper.Db.Updateable<Links>()
                .SetColumnsIF(!String.IsNullOrEmpty(links.name), it => it.name == links.name)
                .SetColumnsIF(!String.IsNullOrEmpty(links.urlname), it => it.urlname == links.urlname)
                .SetColumnsIF(!String.IsNullOrEmpty(links.urlpath), it => it.urlpath == links.urlpath)
                .SetColumnsIF(!String.IsNullOrEmpty(links.iconpath), it => it.iconpath == links.iconpath)
                .SetColumnsIF(links.type > 0, it => it.type == links.type)
                .Where(lt => lt.id == links.id)
                .ExecuteCommand();
        return i > 0;
    }

    public List<Links> SelLinks(Links links)
    {
        var query = SqlSugarHelper.Db.Queryable<Links>();
        query.Where(lt => lt.status.Equals(Static.Enable));
        if (links.type > 0)
        {
            query.Where(lt => lt.type == links.type);
        }
        if (!String.IsNullOrEmpty(links.name))
        {
            query.Where(lt => lt.name.Contains(links.name));
        }
        query.OrderByDescending(lt => lt.num);
        return query.ToList();
    }
}
